package com.fast.common.web.interceptor;

import com.fast.common.web.utils.UserContextHolder;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class UserInterceptor implements HandlerInterceptor {
    public static final String X_CLIENT_TOKEN = "x-client-token";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws JsonProcessingException {
        //从网关获取并校验,通过校验就可信任x-client-token-user中的信息
//        String user = request.getHeader(X_CLIENT_TOKEN);
//        if (user == null) {
//            return true;
//        }
//        UserContextHolder.current().setContext(user);
        return true;
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        UserContextHolder.current().clear();
    }
}